package com.bhs.sansonglogistics.ui.waybill;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.OperatingRecordBean;
import com.bhs.sansonglogistics.bean.OrderGoodsBean;
import com.bhs.sansonglogistics.bean.PictureBean;
import com.bhs.sansonglogistics.bean.PictureShowBean;
import com.bhs.sansonglogistics.bean.WaybillBean;
import com.bhs.sansonglogistics.bean.WaybillDetailsBean;
import com.bhs.sansonglogistics.bean.event.RefreshOrder;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.OperatingRecordAdapter;
import com.bhs.sansonglogistics.ui.adapter.PictureShowAdapter;
import com.bhs.sansonglogistics.ui.adapter.WaybillOrderAdapter;
import com.bhs.sansonglogistics.ui.order.OrderDetailsActivity;
import com.bhs.sansonglogistics.ui.waybill.WaybillReuse;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 运单详情
 */
public class WaybillDetailsActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    // TODO: 2023/4/8  和运单调整有相同字段的放到统一处理类
    private TextView mTvStatus;//运行状态

    private TextView mTvLicensePlateNumber;//车牌号
    private TextView mTvDriver;
    private TextView mTvDriverPhone;
    private LinearLayout mLlAddress;

    private RecyclerView mRvList;//订单
    private WaybillOrderAdapter waybillAdapter;
    private RecyclerView mRvPicture;//照片
    private PictureShowAdapter pictureShowAdapter;

    private RecyclerView mRvOperatingRecord;//操作记录
    private OperatingRecordAdapter operatingRecordAdapter;

    private WaybillReuse waybillReuse = new WaybillReuse();
    private android.widget.Button mBtnBtnAdjustWaybills;
    private android.widget.Button mBtnDerive;
    private android.widget.Button mBtnConfirmArrival;
    private String waybill_sn;
    private final int CONFIRM_ARRIVAL = 102;
    private TextView mTvArriveCompany;//到站公司
    private TextView mTvArriveAddress;//到站地址
    private TextView mTvPrincipal;//负责人
    private TextView mTvArrivePhoneNumber;//负责人手机
    private int type;
    private String status;
    private ArrayList<Object> urlList;

    @Override
    public int initLayout() {
        return R.layout.activity_waybill_details;
    }

    @Override
    public void initView() {
        waybillReuse.initView(this);
        findViewById(R.id.fl_share).setOnClickListener(this);
        mTvStatus = findViewById(R.id.tv_status);
        mTvLicensePlateNumber = findViewById(R.id.tv_license_plate_number);
        mTvDriver = findViewById(R.id.tv_driver);
        mTvDriverPhone = findViewById(R.id.tv_driver_phone);
        mLlAddress = findViewById(R.id.ll_address);
        mRvList = findViewById(R.id.rv_list);
        mRvPicture = findViewById(R.id.rv_picture);
        mRvOperatingRecord = findViewById(R.id.rv_operating_record);
        mBtnBtnAdjustWaybills = findViewById(R.id.btn_btn_adjust_waybills);
        mBtnDerive = findViewById(R.id.btn_derive);
        mBtnConfirmArrival = findViewById(R.id.btn_confirm_arrival);
        mTvArriveCompany = findViewById(R.id.tv_arrive_company);
        mTvArriveAddress = findViewById(R.id.tv_arrive_address);
        mTvPrincipal = findViewById(R.id.tv_principal);
        mTvArrivePhoneNumber = findViewById(R.id.tv_arrive_phone_number);
        mBtnBtnAdjustWaybills.setOnClickListener(this);
        mBtnConfirmArrival.setOnClickListener(this);
        mBtnBtnAdjustWaybills.setOnClickListener(this);
        type = getIntent().getIntExtra("type", 0);
        status = getIntent().getStringExtra("status");
    }

    @Override
    public void initData() {
        waybill_sn = getIntent().getStringExtra("waybill_sn");
        waybillAdapter = new WaybillOrderAdapter();
        mRvList.setAdapter(waybillAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        pictureShowAdapter = new PictureShowAdapter();
        mRvPicture.setAdapter(pictureShowAdapter);
        mRvPicture.setLayoutManager(new GridLayoutManager(mContext, 3));
        operatingRecordAdapter = new OperatingRecordAdapter();
        mRvOperatingRecord.setAdapter(operatingRecordAdapter);
        mRvOperatingRecord.setLayoutManager(new LinearLayoutManager(this));
        if (type == 2) {
            mBtnBtnAdjustWaybills.setVisibility(View.GONE);
        }
        pictureShowAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                new XPopup.Builder(mActivity)
                        .asImageViewer(view.findViewById(R.id.iv_picture), position, urlList, null, new SmartGlideImageLoader())
                        .isShowSaveButton(false)
                        .show();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        networkRequest(netApi.getWaybillDetail(waybill_sn), this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_btn_adjust_waybills) {
            startActivity(new Intent(mActivity, AdjustWaybillActivity.class).putExtras(getIntent()));
        } else if (view.getId() == R.id.btn_confirm_arrival) {
            new XPopup.Builder(mActivity)
                    .asConfirm("提示", "确认到达吗？",
                            "取消", "确定",
                            new OnConfirmListener() {
                                @Override
                                public void onConfirm() {
                                    networkRequest(netApi.waybilConfirmArrival(waybill_sn), WaybillDetailsActivity.this, CONFIRM_ARRIVAL);
                                }
                            }, null, false)
                    .show();
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == CONFIRM_ARRIVAL) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            ToastUtil.show(bean.getMsg());
            if (bean.isStatus()) {
                setStatus();
            }
        } else {
            WaybillDetailsBean waybillDetailsBean = new Gson().fromJson(json, WaybillDetailsBean.class);
            if (waybillDetailsBean.isStatus()) {
                waybillReuse.initData(waybillDetailsBean.getData());
                setData(waybillDetailsBean.getData());
            } else {
                ToastUtil.show(waybillDetailsBean.getMsg());
            }
        }
    }

    private void setData(WaybillDetailsBean.DataDTO waybillData) {
        WaybillDetailsBean.DataDTO.WaybillInfoDTO data = waybillData.getWaybill_info();
        Spanned licensePlateNumber = Html.fromHtml("车牌号：<font color=\"#000000\">" + data.getLicense_plate() + "</font>");
        mTvLicensePlateNumber.setText(licensePlateNumber);
        mTvDriver.setText(String.format("司机：%s", data.getDriver_name()));
        mTvDriverPhone.setText(String.format("手机：%s", data.getDriver_mobile()));
        waybillAdapter.setNewData(waybillData.getDeliver_order_list());
        mTvStatus.setText(waybillData.getWaybill_info().getStatus() == 0 ? "运输中" : "已到达");
        String[] images = data.getImages().split("\\|\\|");
        List<PictureShowBean> pictureShowBeanList = new ArrayList<>();
        urlList = new ArrayList<>();
        for (String image : images) {
            pictureShowBeanList.add(new PictureShowBean(image));
            urlList.add(image);
        }
        pictureShowAdapter.setNewData(pictureShowBeanList);
        if (waybillData.getWaybill_info().getStatus() == 1) {
            setStatus();
        }
        mTvArriveCompany.setText(String.format("到站公司：%s", data.getOutlets_name()));
        mTvPrincipal.setText(String.format("负责人：%s", data.getOutlets_contact()));
        mTvArrivePhoneNumber.setText(String.format("手机：%s", data.getOutlets_mobile()));
        mTvArriveAddress.setText(String.format("到站地址：%s", data.getOutlets_address()));
        operatingRecordAdapter.setNewData(waybillData.getWaybill_log_list());
        waybillAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String deliver_sn = waybillAdapter.getData().get(position).getDeliver_sn();
                startActivity(new Intent(mActivity, OrderDetailsActivity.class).putExtra("deliver_sn", deliver_sn));
            }
        });
    }

    @Override
    public void onFailure(int requestCode) {

    }

    private void setStatus() {
        mBtnBtnAdjustWaybills.setVisibility(View.GONE);
        mBtnConfirmArrival.setVisibility(View.GONE);
    }
}